import os
from selenium import webdriver
from frame.common.util.config_util import config as yml
from frame.common.global_data.path_param import Param


class Browser():
    def __init__(self):
        self.__driver_type = yml.get_driver_value("type")
        self.__driver_ver = yml.get_driver_value("version")
        # path/驱动路径/类型/版本/*driver.exe
        self.__driver_path = os.path.join(
            Param.get_value("path"),
            yml.get_driver_value("path"),
            self.__driver_type,
            self.__driver_ver
        )

    def get_driver(self, b_type=None):
        if b_type is None:
            b_type = self.__driver_path
        return self.__get_driver(b_type)

    def __get_driver(self, b_type="chrome"):
        if b_type == 'chrome':
            driver_path = os.path.join(self.__driver_path, 'chromedriver.exe')
            return webdriver.Chrome(executable_path=driver_path)
        elif b_type == 'firefox':
            driver_path = os.path.join(self.__driver_path, 'geckodriver.exe')
            return webdriver.Firefox(executable_path=driver_path)
        elif b_type == 'edge':
            driver_path = os.path.join(self.__driver_path, 'msedgedriver.exe')
            return webdriver.Edge(executable_path=driver_path)

    # 分布式 远程浏览器驱动  多台电脑分布式并发执行，不同浏览器跑同一套脚本
    # def get_remote_driver(self):  # selenium支持分布式执行grid
    #     pass


if __name__ == '__main__':
    Param.set_value("path", "E:/Python_workspace/auto_test/")

    driver = Browser().get_driver()
    driver.get('https://www.baidu.com')
    driver.close()
